package com.buba.controller;

import com.buba.pojo.Message;
import com.buba.pojo.Pagination;
import com.buba.pojo.UserInfo;
import com.buba.pojo.UserState;
import com.buba.service.UserInfoService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin(allowCredentials = "true",originPatterns = "*")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping("/selUserInfo")
    public List<UserInfo> seAll(){
        return userInfoService.seAll();
    }

    @RequestMapping("/selState")
    public List<UserState> seBll(){
        return userInfoService.seBll();
    }

    @RequestMapping("/staffSel")
    public List<UserInfo> staffSel(@RequestParam Map<String,Object> map){
        return userInfoService.staffSel(map);

    }


    @DeleteMapping("/userinfo")
    public Message del(@RequestParam("userId") String userId){
        int count = userInfoService.del(userId);
        if (count==0)
            return new Message(0,"删除失败");
        return new Message(1,"删除成功");
    }

    @PostMapping("/userinfo")
    public Message ins(@RequestParam Map<String,Object> map){
        int count = userInfoService.ins(map);
        if (count==0)
            return new Message(0,"添加失败");
        return new Message(1,"添加成功");
    }

    @RequestMapping("/userin")
    public Message upd(@RequestParam Map<String,Object> map){
        int count = userInfoService.upd(map);
        System.out.println(map);
        if (count==0)
            return new Message(0,"修改失败");
        return new Message(1,"修改成功");
    }


    @RequestMapping("/selLogg")
    public Pagination<UserInfo> selLogg(@RequestParam Map<String,Object> map){
        Pagination<UserInfo> pagination = new Pagination<>();
        pagination.setPageNow(Long.parseLong(map.get("pageNow")+""));
        pagination.setLimit(Long.parseLong(map.get("limit")+""));
        // 添加分页查询条件
        map.put("index",pagination.computeIndex());   // 添加当前页
        map.put("limit",pagination.getLimit());   // 添加每页条数
        pagination.setPageCount(userInfoService.selLogging(map));
        pagination.computePageTotal();
        pagination.setList(userInfoService.selLoggingList(map));
        System.out.println(pagination);
        return pagination;
    }







    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        //原始文件名
        String originalFilename = file.getOriginalFilename();
        //获取原文件的类型
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
        String fileName = UUID.randomUUID() + suffix;
        //创建一个目录对象，用于存储上传文件
        File dir = new File("D:\\imageS\\");
        //判断当前目录是否存在：目录不存在，需要创建
        if (!dir.exists()) dir.mkdirs();
        try {
            //保存文件到指定目录
            file.transferTo(new File("D:\\images\\" + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //输入流，通过输入流读取文件内容
            bis = new BufferedInputStream(new FileInputStream("D:\\images\\" + name));
            //输出流，通过输出流将文件写回浏览器
            bos = new BufferedOutputStream(response.getOutputStream());
            // 设置相应类型
            response.setContentType("image/png");
            int i = 0;
            while ((i = bis.read()) != -1) {
                bos.write(i);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) bos.close();
                if (bis != null) bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}
